package com.woniuxy.cloud.carl.carlgoods.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.cloud.carl.carlgoods.client.CategoryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.CategoryDto;
import com.woniuxy.cloud.carl.carlgoods.client.param.FindSpuByContionParam;
import com.woniuxy.cloud.carl.carlgoods.client.util.MyPage;
import com.woniuxy.cloud.carl.carlgoods.common.CarlgoodsResultCode;
import com.woniuxy.cloud.carl.carlgoods.infra.mapper.CategoryMapper;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Category;
import com.woniuxy.cloud.carl.carlgoods.service.CategoryService;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin("*")
public class CategoryImpl implements CategoryClient {
    @Resource
    private CategoryService categoryService;
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public Result addCategory(String categoryName) {
        List<Category> categories = categoryService.list();
        for (Category category : categories) {
            if(category.getCategoryName().equals(categoryName)){
                throw new MallException(CarlgoodsResultCode.DATA_NOT_DOUBLE);
            }
        }
        Category category = new Category();
        category.setCategoryName(categoryName);
        categoryService.save(category);
        return Result.success();
    }

    @Override
    public Result<MyPage<CategoryDto>> findCategoryByCondition(FindSpuByContionParam param) {
        Integer pageSize = param.getPageSize();
        String condition = param.getCondition();
        Integer pageNum = param.getPageNum();
        if(pageNum==null||pageSize==null){
            pageNum=1;
            pageSize=3;
        }
        Page<Category> page = new Page<>(pageNum, pageSize);
        MyPage<CategoryDto> myPage= new MyPage<>();
        if(condition==null||condition.equals("")){
            categoryMapper.selectPage(page,null);
        }else {
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("category_name",condition);

            categoryMapper.selectPage(page,queryWrapper);
        }
        List<CategoryDto> categoryDtos = new ArrayList<>();

        for (Category category : page.getRecords()) {

            CategoryDto categoryDto = BeanUtil.toBean(category, CategoryDto.class);

            categoryDtos.add(categoryDto);
        }
        myPage.setPages(page.getPages());
        myPage.setPageNum(page.getCurrent());
        myPage.setTatol(page.getTotal());
        myPage.setPageSize(page.getSize());
        myPage.setList(categoryDtos);
        return Result.success(myPage);
    }

    @Override
    public Result<List<CategoryDto>> listCategory() {
        List<Category> categories = categoryMapper.selectList(null);
        List<CategoryDto> categoryDtos = BeanUtil.copyToList(categories, CategoryDto.class);
        return Result.success(categoryDtos);
    }


}
